package com.zyh.system.JDsupermarkt.dao.impl;

import com.zyh.system.JDsupermarkt.dao.GoodsDao;
import com.zyh.system.JDsupermarkt.domain.Goods;

import java.util.LinkedHashMap;
import java.util.Set;

/**
 * @Author zyh
 * @Date 2024/8/2 17:10
 * @Description GoodSDao 实现类
 */

public class GoodsDaoImpl implements GoodsDao {
    public static final LinkedHashMap<Integer, Goods> GOODS_MAP = new LinkedHashMap<>();

    static {
        GOODS_MAP.put(1111, new Goods("蜘蛛王皮鞋", 238.0, "黑色", 42.2, 10000));
        GOODS_MAP.put(3333, new Goods("ThinkPad x240", 5939.0, "黑色", 12.2, 50));
        GOODS_MAP.put(2222, new Goods("iPhone 7plus", 5686.0, "白色", 5.2, 5000));
    }

    /**
     * 添加商品
     *
     * @param map     集合
     * @param goods   商品对象
     * @param goodsId 商品编号
     * @return 结束方法
     */
    @Override
    public boolean addGoods(LinkedHashMap<Integer, Goods> map, Goods goods, Integer goodsId) {
        map.put(goodsId, goods);
        return true;
    }

    /**
     * 展示所有商品
     *
     * @param map Goods 集合
     */
    @Override
    public void showAll(LinkedHashMap<Integer, Goods> map) {
        System.out.println("编号\t商品名称\t\t\t价格\t颜色\t尺寸\t库存");
        for (Integer key : map.keySet()) {
            Goods value = map.get(key);
            System.out.println(key + "\t" + value.getGoodsName() + "\t\t" + value.getGoodsPrice() + "\t" + value.getGoodsColor() + "\t" + value.getGoodsSize() + "\t" + value.getStock());
        }
    }

    /**
     * 根据id查询对应商品
     *
     * @param map Goods集合
     */
    @Override
    public Goods selectById(LinkedHashMap<Integer, Goods> map, Integer id) {
        Goods goods=null;
        Set<Integer> keySet = map.keySet();
        for (Integer key : keySet) {
            if (key.equals(id)) {
                Goods value = map.get(key);
                goods=value;
                break;
            }
        }
        return goods;
    }

    /**
     * 根据id修改商品
     *
     * @param map Goods集合
     * @param id  商品id
     * @return 结束方法
     */
    @Override
    public boolean updateById(LinkedHashMap<Integer, Goods> map, Integer id, Goods goods) {
        Goods goods1 = selectById(map, id);
        if (goods1!=null) {
            Set<Integer> keySet = map.keySet();
            for (Integer key : keySet) {
                if (key.equals(id)) {
                    map.put(key, goods);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 根据id删除指定商品
     *
     * @param map Goods集合
     * @param id  商品id
     * @return 返回值
     */
    @Override
    public Goods deleteById(LinkedHashMap<Integer, Goods> map, Integer id) {
        Goods remove = map.remove(id);
        return remove;
    }
}